﻿using CodeCity.UserManagement.Service;
using CodeCity.UserManagement.Service.ApplicationModel;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Threading.Tasks;

namespace CodeCity.UserManagement.Admin.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class HouseController : ControllerBase
    {
        private readonly HouseService houseService;

        public HouseController(HouseService houseService)
        {
            this.houseService = houseService;
        }
        [HttpPost("register")]
        public async Task<IActionResult> RegisterAsync([FromBody] HouseRegisterModel model)
        {
            await houseService.RegisterAsync(model);
            return Created("", null);
        }
        [HttpPost("complete")]
        public async Task<IActionResult> CompleteAsync([FromBody] BuildingCompleteModel model)
        {
            await houseService.CompleteAsync(model);
            return Ok();
        }

        [HttpPost("list")]
        public async Task<IActionResult> GetAllAsync()
        {
            var hosiptals = await houseService.LoadAllAsync();
            var results = hosiptals.Select(x => new
            {
                id = x.UnitId,
                x.Name,
                position = x.Position,
                completeTime = x.CompleteTime?.ToString("yyyy-MM-dd HH:mm:ss"),
                humen = $"{x.Humen.Count()}/{x.MaxHumen}",
                buildingTime = x.BuildingTime.ToString("yyyy-MM-dd HH:mm:ss")
            }).ToList();
            return Ok(results);
        }
    }
}
